//
//  PlayView.m
//  27-Block播放按钮练习
//
//  Created by mafeng on 16/8/19.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "PlayView.h"
#import "UIView+Frame.h"
#import "AFNetworking.h"
#import <AVFoundation/AVFoundation.h>

@interface PlayView ()

@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UIButton *nextButton;
@property (nonatomic, strong) UIButton *lastButton;
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UILabel *rightLabel;
@property (nonatomic, copy) void (^play)(UIButton *play);
@property (nonatomic, copy) void (^pause)(UIButton *pause);
@property (nonatomic, copy) void (^next)(UIButton *next);
@property (nonatomic, copy) void (^last)(UIButton *last);
@property (nonatomic, strong) AVAudioPlayer *player;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation PlayView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
        [self createPlayer];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
        [self createPlayer];
    }
    return self;
}

- (void)setUp {
    self.playButton = [self buttonImageWithName:@"player_btn_play_highlight" action:@selector(play:)];
    self.nextButton = [self buttonImageWithName:@"player_btn_next_highlight" action:@selector(next:)];
    self.lastButton = [self buttonImageWithName:@"player_btn_pre_highlight" action:@selector(last:)];
    self.slider = [[UISlider alloc] init];
    [_slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    [_slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_right"] forState:UIControlStateNormal];
    [_slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    [_slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    // 添加定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
    
    self.leftLabel = [[UILabel alloc] init];
    _leftLabel.backgroundColor = [UIColor cyanColor];
    _leftLabel.text = @"mafemg";
    self.rightLabel = [[UILabel alloc] init];
    _rightLabel.backgroundColor = [UIColor cyanColor];
 
    [self addSubview:_playButton];
    [self addSubview:_nextButton];
    [self addSubview:_lastButton];
    [self addSubview:_slider];
    [self addSubview:_leftLabel];
    [self addSubview:_rightLabel];
}

- (void)createPlayer {
//    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//    [manager GET:@"http://fdfs.xmcdn.com/group1/M00/00/3B/wKgDrVCJMObBNiYEAKYB2XLlgPE900.mp3" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//        NSLog(@"%@", downloadProgress);
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
//        dispatch_async(dispatch_get_main_queue(), ^{
//            NSError *error = nil;
//            self.player = [[AVAudioPlayer alloc] initWithData:responseObject error:&error];
//            NSLog(@"%@", self.player);
//        });
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//        NSLog(@"%@", error);
//    }];
    
    // 获取本地的音乐数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"青花瓷" ofType:@"mp3"];
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
    _player.volume = 1.0f;
   // [self.player prepareToPlay];
    // 设置slider的最大值
     _slider.maximumValue = self.player.duration;
    // 设置右边label的数值
     _rightLabel.text = [NSString stringWithFormat:@"%02d:%.2d", (int)self.player.currentTime / 60, (int)self.player.currentTime % 60];
}
 // 定时器事件
- (void)timerAction {
    _slider.value = self.player.currentTime;
    // 设置左边label的数值
     _leftLabel.text = [NSString stringWithFormat:@"%02d:%.2d", (int)self.player.currentTime / 60, (int)self.player.currentTime % 60];
}


- (UIButton *)buttonImageWithName:(NSString *)imageName action:(SEL)action{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)play:(UIButton *)button {
    [button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"%g", self.player.duration);
    // 播放
    [_player play];
    self.play(button);
}

- (void)pause:(UIButton *)button {
    [button setImage:[UIImage imageNamed:@"player_btn_play_highlight"] forState:UIControlStateNormal];
    [button removeTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    // 暂停
    [_player pause];
    
    self.pause(button);
}

- (void)next:(UIButton *)button {
    
    self.next(button);
}

- (void)last:(UIButton *)button {
    
    self.last(button);
}
#pragma mark -- slider的监听事件
- (void)sliderAction:(UISlider *)slider {
    self.player.currentTime = slider.value;
    NSLog(@"%lf", self.player.currentTime);
    // 设置左边label的数值
    _leftLabel.text = [NSString stringWithFormat:@"%02d:%.2d", (int)self.player.currentTime / 60, (int)self.player.currentTime % 60];
    
}



- (void)layoutSubviews {
    [super layoutSubviews];
    _playButton.frame = CGRectMake(0, 0, 100, 100);
    _playButton.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, CGRectGetHeight(self.bounds) / 2);
    
    _nextButton.mf_width = 60;
    _nextButton.mf_height = 60;
    _nextButton.mf_centerY = _playButton.mf_centerY;
    NSLog(@"%f", _playButton.mf_centerY);
    _nextButton.mf_centerX = (_nextButton.mf_width / 2 + _playButton.mf_width / 2 + 60) + _playButton.mf_centerX;
    
    _lastButton.mf_width = 60;
    _lastButton.mf_height = 60;
    _lastButton.mf_centerY = _playButton.mf_centerY;
    _lastButton.mf_centerX =  _playButton.mf_centerX - (_nextButton.mf_width / 2 + _playButton.mf_width / 2 + 60);
    
    _slider.mf_centerY = _playButton.mf_centerY - 60;
    _slider.mf_centerX = _playButton.mf_centerX;
    _slider.mf_width = 300;
    
    _leftLabel.frame = CGRectMake(3, 0, 50, 30);
    _leftLabel.mf_centerY = _slider.mf_centerY;
    
    _rightLabel.frame = CGRectMake(365, 0, 50, 30);
    _rightLabel.mf_centerY = _slider.mf_centerY;
    
}

- (void)play:(void (^)(UIButton *))play pause :(void (^)(UIButton *))pause next:(void (^)(UIButton *))next last:(void (^)(UIButton *))last{
    NSLog(@"%@", play);
    self.play = play;
    self.pause = pause;
    self.next = next;
    self.last = last;
}

@end
